package sma.images.slides.client.slider;

import com.google.gwt.core.client.Scheduler.RepeatingCommand;

public abstract class ImgSliderRepeatingCommand implements RepeatingCommand, ISlidesCtrl {
	private boolean isPlaying = true;
	private int currentImgIndex=0;
	private int nbSlides;
	
	public ImgSliderRepeatingCommand(int nbSlides_){
		nbSlides = nbSlides_;
	}
	
	@Override
	public boolean execute() {
		if(isPlaying){
			showNext();
		}
		return true;
	}
	
	public void pause(){
		isPlaying = false;
	}
	
	public void play(){
		showNext();
		isPlaying = true;
	}
	
	public void showNext(){
		currentImgIndex = getNextIndex(getCurrentIndex());
		showCurrent(getCurrentIndex());
	}
	
	public void showPrevious(){
		currentImgIndex = getPreviousIndex(getCurrentIndex());
		showCurrent(getCurrentIndex());
	}
	
	protected abstract void showCurrent(int currentSlide_);

	@Override
	public void showSlide(int num_) {
		if(getCurrentIndex()>=0 && getCurrentIndex()<nbSlides){
			currentImgIndex=num_;
			showCurrent(getCurrentIndex());
		}
	}
	
	@Override
	public int getCurrentIndex(){
		return currentImgIndex;
	}
	
	@Override
	public int getPreviousIndex(int slideIndex_){
		int currentI = slideIndex_;
		currentI--;
		if(currentI<0){
			currentI=nbSlides-1;
		}
		return currentI;
	}
	
	@Override
	public int getNextIndex(int slideIndex_){
		int currentI = slideIndex_;
		currentI++;
		if(currentI>=nbSlides){
			currentI=0;
		}
		return currentI;
	}

	@Override
	public int getSlidesCount() {
		return nbSlides;
	}
	
	
}
